package com.virtualmall.listener;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

//import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.virtualmall.bean.Account;
import com.virtualmall.common.Constants;
import com.virtualmall.service.AccountService;

/**
 * session监听
 * 
 */
public class SessionEventListener implements HttpSessionListener {

    //private static Logger logger = Logger.getLogger(SessionEventListener.class);

    @Autowired
    private AccountService accountService;

    public void sessionCreated(HttpSessionEvent httpsessionevent) {
        // TODO Auto-generated method stub

    }

    public void sessionDestroyed(HttpSessionEvent httpsessionevent) {
        //logger.info("sessionDestroyed() begin.");
        HttpSession session = httpsessionevent.getSession();

        Account account = this.accountService.read((Long) session.getAttribute(Constants.USER_ID_SESSION));

        account.setOnlineStatus(Constants.ACCOUNT_STATUS_OFFLINE);
        this.accountService.update(account);

    }
}
